//
//  MeQrCodeViewController.m
//  YSSmartHome
//
//  Created by yanghao on 2017/8/16.
//  Copyright © 2017年 YangHao. All rights reserved.
//

#import "MeQrCodeViewController.h"
#import "YSCommon.h"
#import "MQTTSessionManager.h"
#import "QrCodeUtils.h"

@interface MeQrCodeViewController ()
@property (nullable, nonatomic, strong) UIImageView* avatar;
@property (nullable, nonatomic, strong) UIView* contentView;
@property (nullable, nonatomic, strong) UIImageView* qrCodeImageView;
@property (nullable, nonatomic, strong) UILabel* tipsLabel;
@end

@implementation MeQrCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSubviews];
    XAITYPEAPSN apsn = kMQTTSessionManager.currentUser.apsn;
    XAITYPELUID luid = kMQTTSessionManager.currentUser.luid;
    NSString * qrCodeStr = [[NSString alloc] initWithFormat:@"Friend,%zd,%zd",apsn, luid];
    UIImage* qrImage = [QrCodeUtils generateQrCodeImage:qrCodeStr withSize:200];
    self.qrCodeImageView.image = qrImage;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - private method 
- (void)initSubviews {
    self.title = @"我的二维码";
    self.view.backgroundColor = kBgColor;
    [self.view addSubview:self.contentView];
    [self.view addSubview:self.avatar];
    [self.contentView addSubview:self.qrCodeImageView];
    [self.contentView addSubview:self.tipsLabel];
    [self layoutPagesubviews];
}

- (void)layoutPagesubviews {
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.leading.equalTo(self.view).offset(kIphoneSize_Widith(30));
        make.trailing.equalTo(self.view).offset(kIphoneSize_Widith(-30));
        make.height.equalTo(kIphoneSize_Height(400));
    }];
    [self.avatar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView.top);
        make.centerX.equalTo(self.contentView);
        make.width.height.equalTo(100);
    }];
    [self.tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(self.contentView);
    }];
    [self.qrCodeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.contentView);
        make.top.equalTo(50);
        make.bottom.equalTo(self.tipsLabel.top);
    }];
}
#pragma mark - getters and setters

- (UIImageView *)avatar {
    if (!_avatar) {
        _avatar = [[UIImageView alloc]init];
        _avatar.image = [UIImage imageNamed:@"Avatar_me"];
    }
    return _avatar;
}

- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc]init];
        _contentView.backgroundColor = [UIColor whiteColor];
    }
    return _contentView;
}
- (UIImageView *)qrCodeImageView
{
    if (!_qrCodeImageView) {
        _qrCodeImageView = [[UIImageView alloc]init];
    }
    return _qrCodeImageView;
}

- (UILabel *)tipsLabel {
    if (!_tipsLabel) {
        _tipsLabel = [[UILabel alloc]init];

        NSMutableAttributedString* attributrStr = [[NSMutableAttributedString alloc]initWithString:@"扫一扫二维码图案, 加我好友" attributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:13]}];
        NSAttributedString* addAttributrStr = [[NSAttributedString alloc] initWithString:@"共享智能" attributes:@{NSForegroundColorAttributeName: [UIColor blueColor], NSFontAttributeName:[UIFont systemFontOfSize:13]} ];
         [attributrStr appendAttributedString: addAttributrStr];
        _tipsLabel.attributedText = attributrStr;
        _tipsLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _tipsLabel;
}
@end
